package com.ailink.spring;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Slf4j
@Component
public class MyLifecycleComponent implements BeanNameAware,
        BeanFactoryAware,
        BeanClassLoaderAware,
        ApplicationContextAware,
        InitializingBean {
    public MyLifecycleComponent() {
        log.info("MyLifecycleComponent");
    }

    @PostConstruct
    public void init() {
        log.info("PostConstruct");
    }

    @PreDestroy
    public void destroy() {
        log.info("PostConstruct");
    }

    @Override
    public void setBeanName(String name) {
        log.info("BeanNameAware {}", name);
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        log.info("BeanFactoryAware {}", beanFactory);
    }

    @Override
    public void setBeanClassLoader(ClassLoader classLoader) {
        log.info("BeanClassLoaderAware {}", classLoader);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        log.info("ApplicationContextAware {}", applicationContext);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        log.info("InitializingBean");
    }
}
